package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessFarmSystem;
import com.digitalchina.lxnsy.api.BusinessShareFarm;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.Constant;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.AddressVO;
import com.digitalchina.lxnsy.entity.OrderItemVO;
import com.digitalchina.lxnsy.entity.OrderVO;
import com.digitalchina.lxnsy.entity.PayVO;
import com.digitalchina.lxnsy.entity.PersonalBaseInfoVO;
import com.digitalchina.lxnsy.entity.PostageVO;
import com.digitalchina.lxnsy.entity.ShareFarmListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.MathExtend;
import com.digitalchina.lxnsy.wxapi.WXPayEntryActivity;
import com.facebook.drawee.view.SimpleDraweeView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/9/20 17:17
 * 修改备注：
 * ================================================
 */
public class SubmitOrderShareActivity extends BaseActivity {
    @Bind(R.id.tv_post)
    TextView tvPost;
    @Bind(R.id.tv_take)
    TextView tvTake;
    @Bind(R.id.tv_address_type)
    TextView tvAddressType;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_mobile)
    TextView tvMobile;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.layout_address)
    LinearLayout layoutAddress;
    @Bind(R.id.sdv_logo)
    SimpleDraweeView sdvLogo;
    @Bind(R.id.tv_farm_name)
    TextView tvFarmName;
    @Bind(R.id.sdv_goods_image)
    SimpleDraweeView sdvGoodsImage;
    @Bind(R.id.tv_goods_name)
    TextView tvGoodsName;
    @Bind(R.id.tv_goods_price)
    TextView tvGoodsPrice;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.iv_minus)
    ImageView ivMinus;
    @Bind(R.id.tv_number)
    TextView tvNumber;
    @Bind(R.id.iv_plus)
    ImageView ivPlus;
    @Bind(R.id.iv_minus2)
    ImageView ivMinus2;
    @Bind(R.id.tv_number2)
    TextView tvNumber2;
    @Bind(R.id.iv_plus2)
    ImageView ivPlus2;
    @Bind(R.id.tv_bzj)
    TextView tvBzj;
    @Bind(R.id.tv_msg)
    EditText tvMsg;
    @Bind(R.id.tv_total_price)
    TextView tvTotalPrice;
    @Bind(R.id.tv_total_tag)
    TextView tvTotalTag;
    @Bind(R.id.tv_postage)
    TextView tvPostage;
    @Bind(R.id.tv_total_price2)
    TextView tvTotalPrice2;
    @Bind(R.id.btn_submit)
    Button btnSubmit;
    @Bind(R.id.ll_submit_order_bottom_layout)
    LinearLayout llSubmitOrderBottomLayout;
    @Bind(R.id.layout_dwgs)
    RelativeLayout layoutDwgs;
    @Bind(R.id.layout_bzj)
    RelativeLayout layoutBzj;
    @Bind(R.id.layout_people)
    LinearLayout layoutPeople;
    @Bind(R.id.tv_address_take)
    TextView tvAddressTake;

    private ShareFarmListVO vo;


    private String consignee = "";//       收货人
    private String shippingAddress = "";//   收货地址详细地址
    private String shippingPhone = "";//    联系电话
    private String tip = "";//      买家备注
    private String totalEnsureMoney = "";//  总保证金字段
    private String orderType = "";//       订单类型 0预订 1直接购买 ，2出租, 3出售
    private String orderVOListJson = "";//    订单 列表json格式

    private List<OrderItemVO> orderItemVOList = new ArrayList<>();
    private OrderItemVO orderItemVOs; //订单项
    private List<OrderVO> orderVOList = new ArrayList<>();
    private OrderVO orderVO;//订单信息
    private boolean isPostFree;
    private BigDecimal postPrice;//邮费
    private AddressVO addressVO;//返回的选择地址
    private String provinceName, cityName, countyName, detailsName, proCode, cityCode;

    private String productId;//商品ID
    private String attrName;// 商品属性  共享 出售
    private String delivery = ""; // 0-邮寄  1-自提

    private String price = ""; // 商品单价  共享和出售不同
    private String gxPrice = ""; // 共享
    private String csPrice = ""; // 出售

    private List<AddressVO> mAddressList;

    private String sellerName = "";
    private String sellerPhone = "";
    private String sellerLogo = "";
    private String sellerAddress = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_submit_order_share);
        setTitle("确认订单");
    }

    @Override
    public void initMember() {

        // 获取地址列表
        BusinessFarmSystem.getAddressList(this, mHandler);
        tvAddress.setText("请选择收货地址");

        vo = (ShareFarmListVO) getIntent().getSerializableExtra("ShareFarmListVO");

        BusinessAccount.getPersonInfoById(this, vo.getUserAccId(), mHandler);

        setView();
    }

    private void setView() {
/*

        productId = vo.getId();

        delivery = vo.getSubmit().getDelivery();


        for (int i = 0; i < vo.getAgricultureProductPriceList().size(); i++) {
            if (vo.getAgricultureProductPriceList().get(i).getAttrName().equals("出售")) {
                csPrice = String.valueOf(vo.getAgricultureProductPriceList().get(i).getPrice());
            } else {
                gxPrice = String.valueOf(vo.getAgricultureProductPriceList().get(i).getPrice());
            }
        }

        String strNum = vo.getSubmit().getNum1(); // 购买个数
        String strNum2 = vo.getSubmit().getNum2(); // 单位个数
        int num = Integer.parseInt(strNum);
        int num2 = Integer.parseInt(strNum2);
        // 0-租借 1-购买
        if (vo.getSubmit().getType().equals("0")) {
            orderType = "2";
            // 购买数量
            tvNumber.setText(strNum);
            // 单位个数
            tvNumber2.setText(strNum2);
            // 保证金
            tvBzj.setText("￥" + vo.getEnsureMoney() + "元");
            // 起租日期
            tvDate.setText(vo.getSubmit().getDate());

            attrName = "共享";

            price = gxPrice;
        } else {
            orderType = "3";
            layoutDwgs.setVisibility(View.GONE);
            layoutBzj.setVisibility(View.GONE);
            // 购买个数
            tvNumber.setText(strNum);

            tvBzj.setVisibility(View.GONE);
            layoutDwgs.setVisibility(View.GONE);

            attrName = "出售";

            price = csPrice;
        }

        // 邮费 isFreePostage = 1 为包邮
        if (vo.getIsFreePostage() == null || "1".equals(vo.getIsFreePostage())) {
            isPostFree = true;
            postPrice = new BigDecimal("0.00");
            tvPostage.setText("包邮");
            BigDecimal price1 = new BigDecimal(MathExtend.multiply(String.valueOf(num * num2), price)).setScale(2, BigDecimal.ROUND_HALF_UP);
            tvTotalPrice.setText(String.format("￥ %s", price1));
            tvTotalPrice2.setText(String.format("￥ %s", price1));
        } else {
            // 邮寄
            if ("0".equals(delivery)) {
                isPostFree = false;
                computePostAge();
//                postPrice = new BigDecimal(vo.getPostage());
//                tvPostage.setText(String.format("（含运费%s元）", postPrice.setScale(2, BigDecimal.ROUND_HALF_UP)));
//                BigDecimal price1 = new BigDecimal(MathExtend.add(MathExtend.multiply(String.valueOf(num * num2), price), String.valueOf(postPrice))).setScale(2, BigDecimal.ROUND_HALF_UP);
//                tvTotalPrice.setText(String.format("￥ %s", price1));
//                tvTotalPrice2.setText(String.format("￥ %s", price1));
            } else {
                // 自提
                isPostFree = true;
                postPrice = new BigDecimal("0.00");
                tvPostage.setText("自提免运费");
                BigDecimal price1 = new BigDecimal(MathExtend.multiply(String.valueOf(num * num2), price)).setScale(2, BigDecimal.ROUND_HALF_UP);
                tvTotalPrice.setText(String.format("￥ %s", price1));
                tvTotalPrice2.setText(String.format("￥ %s", price1));
            }

        }


        // 自提 邮寄都支持
        if ("1".equals(vo.getHasPostMethod()) && "1".equals(vo.getHasTakenMethod())) {
            // 上个页面传过来得到参数 0-邮寄  1-自提
            if ("0".equals(delivery)) {
                tvAddressType.setText("收货地址");
                tvAddressTake.setVisibility(View.GONE);
                tvAddress.setVisibility(View.VISIBLE);
                tvPost.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_select_bg);
                tvPost.setTextColor(getResources().getColor(R.color.colorRed));
                tvTake.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_unselect_bg);
                tvTake.setTextColor(getResources().getColor(R.color.colorGray));
            } else {
                tvAddressType.setText("自提地址");
                tvAddressTake.setVisibility(View.VISIBLE);
                tvAddress.setVisibility(View.GONE);
                layoutPeople.setVisibility(View.GONE);
                tvTake.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_select_bg);
                tvTake.setTextColor(getResources().getColor(R.color.colorRed));
                tvPost.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_unselect_bg);
                tvPost.setTextColor(getResources().getColor(R.color.colorGray));
            }
            tvAddressTake.setText(vo.getRetentionAddress());
        } else {
            // 是否有邮寄方式
            if ("1".equals(vo.getHasPostMethod())) {
                tvAddressType.setText("收货地址");
                tvPost.setVisibility(View.VISIBLE);
                tvAddressTake.setVisibility(View.GONE);
                tvAddress.setVisibility(View.VISIBLE);
                tvPost.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_select_bg);
                tvPost.setTextColor(getResources().getColor(R.color.colorRed));
            } else
                tvPost.setVisibility(View.INVISIBLE);

            // 是否有自提方式
            if ("1".equals(vo.getHasTakenMethod())) {
                tvAddressType.setText("自提地址");
                layoutPeople.setVisibility(View.GONE);
                tvTake.setVisibility(View.VISIBLE);
                tvAddressTake.setVisibility(View.VISIBLE);
                tvAddress.setVisibility(View.GONE);
                tvTake.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_select_bg);
                tvTake.setTextColor(getResources().getColor(R.color.colorRed));
                tvAddressTake.setText(vo.getRetentionAddress());

            } else
                tvTake.setVisibility(View.GONE);
        }


        if (vo.getProductImageList() != null && vo.getProductImageList().size() > 0)
            FrescoUtil.showImageSmall(vo.getProductImageList().get(0).getImage(), sdvGoodsImage);

        tvGoodsName.setText(vo.getName());

        tvGoodsPrice.setText("￥" + price);

*/

    }


    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SHARE_GENERATE_ORDERS_SUCCESS:
                        List<String> orderNoList = FastJsonUtil.getListBean(msg.obj.toString(), "orderNoList", String.class);
                        String orderNo = orderNoList != null ? orderNoList.get(0) : "";

                        PayVO payVO = new PayVO();
                        payVO.setPrice(orderVO.getTotalPriceReal());
                        payVO.setOrderNO(orderNo);
                        payVO.setType(2);
                        GotoUtil.gotoActivity(SubmitOrderShareActivity.this, WXPayEntryActivity.class, true, "payVO", payVO);
                        break;
                    case MSG.MSG_SHARE_GENERATE_ORDERS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_ADDRESS_LIST_SUCCESS: // 获取地址列表
                        setAddressList(msg.obj);
                        break;
                    case MSG.MSG_COMPUTE_POSTAGE_FOR_PRODUCT_JSON_SUCCESS://快递用费计算
                        String price = FastJsonUtil.getString(msg.obj.toString(), "data");
                        postPrice = new BigDecimal(price);
                        setNumAndPrice();
                        break;
                    case MSG.MSG_IS_ALL_SHARING_SCOPE_SUCCESS: // 地址范围判断
                        submit();
                        break;
                    case MSG.MSG_IS_ALL_SHARING_SCOPE_FIELD:
                        showToast("很抱歉，您的收货地址不在共享范围");
                        break;
                    case MSG.MSG_GET_PERSON_INFO_SUCCESS: // 个人信息
                        PersonalBaseInfoVO vo = FastJsonUtil.getBean(msg.obj.toString(), "personalBaseInfo", PersonalBaseInfoVO.class);

                        if (vo != null) {
                            sellerName = vo.getUserName();
                            sellerPhone = vo.getMobilePhone();
                            sellerLogo = vo.getHeadPortrait();
//                            sellerAddress = vo.getAddressDetail();
                        }


                        break;
                }
            }
        };
    }

    /**
     * 根据商品数量 计算商品总价
     */
    private void setNumAndPrice() {
        int num = Integer.parseInt(tvNumber.getText().toString());
        int num2 = Integer.parseInt(tvNumber2.getText().toString());
        String price1 = String.format("￥ %s", new BigDecimal(MathExtend.add(MathExtend.multiply(String.valueOf(num * num2), price), String.valueOf(postPrice))).setScale(2, BigDecimal.ROUND_HALF_UP));
        tvTotalPrice.setText(price1);
        tvTotalPrice2.setText(price1);
        tvPostage.setText(String.format("（含运费%s元）", postPrice.setScale(2, BigDecimal.ROUND_HALF_UP)));
    }

    private void setAddressList(Object obj) {
        mAddressList = FastJsonUtil.getListBean(obj.toString(), "body", "addressList", AddressVO.class);

        // 如果是邮寄
        if ("0".equals(delivery)) {
            if (mAddressList != null && mAddressList.size() > 0) {
                layoutPeople.setVisibility(View.VISIBLE);
                addressVO = mAddressList.get(0);
                tvName.setText(addressVO.getUserName());
                tvMobile.setText(addressVO.getMobilePhone());
                displayAddress(addressVO, tvAddress);
            } else {
                layoutPeople.setVisibility(View.GONE);
                tvAddress.setText("请选择收货地址");
            }
        }
    }

    /**
     * 地址展示
     */
    private void displayAddress(AddressVO data, TextView tvAddress) {


        if (null == data.getProvince()) {
            provinceName = "";
        } else {
            proCode = data.getProvince().getCode();
            provinceName = data.getProvince().getProvinceName();
        }

        if (null == data.getCity()) {
            cityName = "";
        } else {
            cityCode = data.getCity().getCode();
            cityName = data.getCity().getCityName();
        }

        if (null == data.getCounty())
            countyName = "";
        else
            countyName = data.getCounty().getCountyName();

        if (null == data.getAddress())
            detailsName = "";
        else
            detailsName = data.getAddress();

        if (proCode.equals("110000") || proCode.equals("120000") || proCode.equals("310000") || proCode.equals("500000") || (proCode.equals("460000") && cityCode.equals("469000")))
            tvAddress.setText(String.format("%s %s %s", provinceName, countyName, detailsName));
        else
            tvAddress.setText(String.format("%s %s %s %s", provinceName, cityName, countyName, detailsName));

    }


    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_post, R.id.tv_take, R.id.layout_address, R.id.iv_minus, R.id.iv_plus, R.id.iv_minus2, R.id.iv_plus2, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_post: // 邮寄
                delivery = "0";
                tvAddressType.setText("收货地址");
                tvPost.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_select_bg);
                tvPost.setTextColor(getResources().getColor(R.color.colorRed));
                tvTake.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_unselect_bg);
                tvTake.setTextColor(getResources().getColor(R.color.colorGray));
                if (mAddressList != null && mAddressList.size() > 0)
                    layoutPeople.setVisibility(View.VISIBLE);
                tvAddress.setVisibility(View.VISIBLE);
                tvAddressTake.setVisibility(View.GONE);
                computePostAge();
                break;
            case R.id.tv_take: // 自提
                delivery = "1";
                tvAddressType.setText("自提地址");
                tvTake.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_select_bg);
                tvTake.setTextColor(getResources().getColor(R.color.colorRed));
                tvPost.setBackgroundResource(R.drawable.submit_order_rectangle_with_radius_unselect_bg);
                tvPost.setTextColor(getResources().getColor(R.color.colorGray));
                layoutPeople.setVisibility(View.GONE);
                tvAddress.setVisibility(View.GONE);
                tvAddressTake.setVisibility(View.VISIBLE);
                isPostFree = true;
                postPrice = new BigDecimal("0.00");
                tvPostage.setText("自提免运费");
                int num = Integer.parseInt(tvNumber.getText().toString());
                int num2 = Integer.parseInt(tvNumber2.getText().toString());
                BigDecimal price1 = new BigDecimal(MathExtend.multiply(String.valueOf(num * num2), price)).setScale(2, BigDecimal.ROUND_HALF_UP);
                tvTotalPrice.setText(String.format("￥ %s", price1));
                tvTotalPrice2.setText(String.format("￥ %s", price1));
                break;
            case R.id.layout_address:
                if (delivery.equals("0"))
                    GotoUtil.gotoForResultActivity(SubmitOrderShareActivity.this, MyAddressListActivity.class, 99, "isShowCheck", true);
                break;
            case R.id.iv_minus:
                int i = Integer.parseInt(tvNumber.getText().toString());
                if (i > 1) {
                    i--;
                    tvNumber.setText(i + "");
                    computePostAge();
                }
                break;
            case R.id.iv_plus:
                int i1 = Integer.parseInt(tvNumber.getText().toString());
                i1++;
                tvNumber.setText(i1 + "");
                computePostAge();
                break;
            case R.id.iv_minus2:
                int i2 = Integer.parseInt(tvNumber2.getText().toString());
                if (i2 > 1)
                    i2--;
                tvNumber2.setText(i2 + "");
                computePostAge();
                break;
            case R.id.iv_plus2:
                int i3 = Integer.parseInt(tvNumber2.getText().toString());
                i3++;
                tvNumber2.setText(i3 + "");
                computePostAge();
                break;
            case R.id.btn_submit:
                if (delivery.equals("0")) {
                    if (layoutPeople.getVisibility() == View.VISIBLE) {
                        BusinessShareFarm.isAllSharingScope(SubmitOrderShareActivity.this, productId, provinceName, cityName, countyName, mHandler);
                    } else {
                        showToast("请选择收货地址");
                    }
                } else {
                    submit();
                }

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == RESULT_OK)
            addressVO = (AddressVO) data.getSerializableExtra("addressVO");

        if (null != addressVO) {
            layoutPeople.setVisibility(View.VISIBLE);
            tvName.setText(addressVO.getUserName());
            tvMobile.setText(addressVO.getMobilePhone());
            provinceName = addressVO.getProvince().getName();
            cityName = addressVO.getCity() != null ? addressVO.getCity().getName() : "";
            countyName = addressVO.getCounty() != null ? addressVO.getCounty().getName() : "";
            detailsName = addressVO.getAddress();
            tvAddress.setText(String.format("%s %s %s %s", provinceName,
                    cityName, countyName, detailsName));
        }
    }

    /**
     * 共享 生成订单
     * consignee        收货人
     * shippingProvince 收货地址省
     * shippingCity     收货地址市
     * shippingCounty   收货地址区县
     * shippingDetail   收货地址详细地址
     * shippingPhone    联系电话
     * tip              买家备注
     * totalEnsureMoney 总保证金字段
     * orderType        订单类型 0预订 1直接购买 ，2出租, 3出售
     * orderVOList      订单 列表json格式
     */
    private void submit() {
        if (orderItemVOList != null)//防止订单提交失败 重复添加数据
            orderItemVOList.clear();
        if (orderVOList != null)
            orderVOList.clear();

        int num = Integer.parseInt(tvNumber.getText().toString());
        int num2 = Integer.parseInt(tvNumber2.getText().toString());
        // 单商品购买 订单项和商品项价格统一
        String totalPrice = String.valueOf(new BigDecimal(MathExtend.add(MathExtend.multiply(String.valueOf(num * num2), price), String.valueOf(postPrice))).setScale(2, BigDecimal.ROUND_HALF_UP));
        //现售
        orderItemVOs = new OrderItemVO();
        orderItemVOs.setProductId(vo.getId());
        orderItemVOs.setProductType("0");//商品类型
        orderItemVOs.setProductName(vo.getName());
        orderItemVOs.setProductDesc("");
        orderItemVOs.setPrice(String.valueOf(new BigDecimal(price)));
        orderItemVOs.setPreferentialPrice(String.valueOf(new BigDecimal(price)));
        orderItemVOs.setTotalPrice(totalPrice);
        orderItemVOs.setTotalPriceReal(totalPrice);
        orderItemVOs.setIsGift("false");
        orderItemVOs.setQuantity(tvNumber.getText().toString());
        orderItemVOs.setUnit(vo.getUnit() != null && vo.getUnit().getName() != null ? vo.getUnit().getName() : "");
        orderItemVOs.setIsMailFree(String.valueOf(isPostFree));
        orderItemVOs.setPriceType("1"); // 属性定价

        // 保证金
        orderItemVOs.setEnsureMoney(vo.getEnsureMoney()+"");
        // 起租日期
        //orderItemVOs.setRentDate(vo.getSubmit().getDate());

        orderItemVOs.setAttrName(attrName);
        if (orderType.equals("2"))
            orderItemVOs.setUnitMumber(tvNumber2.getText().toString());

        orderItemVOs.setFreightPrice(String.valueOf(postPrice));
        orderItemVOs.setFreightPriceReal(String.valueOf(postPrice));

//        orderItemVOs.setRetainDays(vo.getRetentionTime());
        //TODO 动态计算
        orderItemVOs.setWeight("");
        //TODO 拼接地址
        orderItemVOs.setProductUrl(URL.URL_GET_PRODUCT_INFO + "?productId=" + vo.getId());
        // 商品分类
        orderItemVOs.setCommodityType(vo.getType().getName());
        orderItemVOList.add(orderItemVOs);

        //拆单并生成订单
        orderVO = new OrderVO();
        orderVO.setPickupAddress(vo.getPickUpAddress());//取货地址
        orderVO.setTip(tvMsg.getText().toString());//买家备注
        orderVO.setPaymentType(Constant.PAYMENT_TYPE_ON_LINE);//支付类型 线下支付，在线支付
        if (delivery.equals("0"))
            orderVO.setShippingType(Constant.SHIPPING_TYPE_POST); //发货类型：邮寄
        else
            orderVO.setShippingType(Constant.SHIPPING_TYPE_TAKE_THEIR); // 自提
        orderVO.setTotalPriceOrigin(totalPrice);/* 应收总价 */
        orderVO.setTotalPriceReal(totalPrice);/* 实收总价 */
        orderVO.setSellerId(vo.getUserAccId());//卖家ID
        orderVO.setSellerName(sellerName);//卖家名称
        orderVO.setSellerPhone(sellerPhone);//卖家电话
        orderVO.setSellerLogo(sellerLogo);//卖家logo
        orderVO.setSellerAddress(sellerAddress);//卖家地址
        orderVO.setIsInvoice("false");//是否开具发票
        orderVO.setInvoiceTitle("");//发票抬头
        //TODO 预售才有
        orderVO.setAppointShippingAt("");//约定发货（取货）时间

        orderVO.setOrderItemVOList(orderItemVOList);//订单项
        orderVO.setFee(String.valueOf(new BigDecimal(0)));//手续费
        orderVO.setCoupon("");//优惠码
        orderVO.setCouponPrice(String.valueOf(new BigDecimal(0)));//优惠金额
        orderVO.setPaidAmount(String.valueOf(new BigDecimal(0)));//已付金额
        orderVO.setAdjustAmount(String.valueOf(new BigDecimal(0)));//调整金额
        if (isPostFree) {
            orderVO.setFreightPriceOrigin(String.valueOf(new BigDecimal("0.00")));/* 应收总运费 */
            orderVO.setFreightPriceReal(String.valueOf(new BigDecimal("0.00")));/* 实收总运费 */
        } else {
            orderVO.setFreightPriceOrigin(String.valueOf(postPrice));/* 应收总运费 */
            orderVO.setFreightPriceReal(String.valueOf(postPrice));/* 实收总运费 */
        }

        orderVO.setDeductIntegral(String.valueOf(0));/* 抵扣积分 */
        orderVO.setDeductAmount(String.valueOf(new BigDecimal(0)));/* 抵扣金额 */
        orderVOList.add(orderVO);

        orderVOListJson = JSON.toJSONString(orderVOList, true);

        consignee = tvName.getText().toString();
        shippingAddress = detailsName;
        shippingPhone = tvMobile.getText().toString();
        tip = tvMsg.getText().toString();
        totalEnsureMoney = vo.getEnsureMoney()+"";
        BusinessShareFarm.generateOrders(this, consignee, provinceName, cityName, countyName, shippingAddress, shippingPhone, tip, totalEnsureMoney,
                orderType, orderVOListJson, mHandler);
    }


    /**
     * 计算快递费用
     */
    private void computePostAge() {

        // 邮寄并且有邮费
        if ("0".equals(vo.getIsFreePostage()) && "0".equals(delivery)) {
            isPostFree = false;
            PostageVO postageVO = new PostageVO();
            postageVO.setProductId(productId);
            postageVO.setNum(Integer.parseInt(tvNumber.getText().toString()));
            List<PostageVO> list = new ArrayList<>();

            list.add(postageVO);
            JSONArray jsonArray = new JSONArray();

            for (int i = 0; i < list.size(); i++) {
                JSONObject jsonObject = (JSONObject) JSON.toJSON(list.get(i));
                jsonArray.add(jsonObject);
            }


            BusinessFarmSystem.computePostageForProductJson(SubmitOrderShareActivity.this, jsonArray.toJSONString(), mHandler);
        } else {
            isPostFree = true;
            postPrice = new BigDecimal("0.00");
            int num = Integer.parseInt(tvNumber.getText().toString());
            int num2 = Integer.parseInt(tvNumber2.getText().toString());
            String price1 = String.valueOf(new BigDecimal(MathExtend.multiply(String.valueOf(num * num2), price)));
            tvPostage.setText("包邮");
            tvTotalPrice.setText(price1);
            tvTotalPrice2.setText(price1);
        }

    }
}
